﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace LicPdaService
{
    public class PDAXmlPutPackProcess
    {
        /// <summary>
        /// 2.1原料上架分配 Add By Quinn.Ling 201705
        /// </summary>
        public static string GetPutPack(string xml)
        {
            #region 参数源
            List<string> sqllists = new List<string>();
            PDAXmlCheckProcess.status_check = "true";
            string strSql = string.Empty;
            string lspn = string.Empty;
            string sku = string.Empty;
            string asn = string.Empty;
            string lsdesc = "";
            string lsunit = "";
            string lslottable01 = "";
            string lslottable02 = "";
            string lslottable03 = "";
            string lslottable04 = "";
            string lslottable05 = "";
            string lslottable06 = "";
            string lslottable07 = "";
            string lslottable08 = "";
            string lslottable09 = "";
            string lslottable10 = "";
            string storage = "";
            string lsline_no = "";
            string lscustomer_code = string.Empty;
            string lottable01 = string.Empty;
            string lottable02 = string.Empty;
            string line_no = string.Empty;
            double qty = 0; //本次上架数量
            double qty_yb = 0; //预报数量
            double qty_sm = 0; //累计已上架数量
            double unit_price = 0; //单价
            double total_price = 0; //总价
            double unit_weight = 0; //单重
            double net_weight = 0; //净重
            App.Connection();
            DataTable ldt = LicPdaService.App.Xml2DataTable(xml);
            if (ldt.Rows.Count == 0)
            {
                return "";
            }
            App.swhseid = ldt.Rows[0]["whseid"].ToString();
            App.suserid = ldt.Rows[0]["userid"].ToString();
            App.sasn = ldt.Rows[0]["asn"].ToString();
            storage= ldt.Rows[0]["storage"].ToString();

            strSql = "select recstatus.status,receipt.shipkey,data_from,receipt.customer_code from receipt left join recstatus on receipt.status=recstatus.status where recstatus.status in('01','02','03','04','05','06')  and receipt.whseid='" + App.swhseid + "' and receiptkey='" + App.sasn + "' ";
            DataTable ldt_asn = App.GetDataTable(App.DBconWMS, strSql, "Sku");
            if (ldt_asn.Rows.Count > 0)
            {
                lscustomer_code = ldt_asn.Rows[0]["customer_code"].ToString();
            }


            //2017-04-06增加，先验收完成后，再判断上架信息
            strSql = "select * from  ReceiptDetail where (status in ('01','02','03','04','05') OR status IS NULL) and  whseid='" + App.swhseid + "' and receiptkey='" + App.sasn + "'";
            DataTable ldt_asn_detail = App.GetDataTable(App.DBconWMS, strSql, "Sku");

            DataTable ldt_dy = App.GetNewDataTabel(ldt);
            if (ldt_asn_detail.Rows.Count == 0)
            {
                for (int j = 0; j < ldt_dy.Rows.Count; j++)
                {
                    ldt_dy.Rows[j]["msg"] = "扫描的单号："+ App.sasn + "在ASN中未发现要上架的信息！ 可能原因：1、无ASN预报信息  2、ASN已全部上架完成，不可重复上架";
                }
                return GetPrint_Return(ldt_dy);
            }
            #endregion

            #region 4.1预报信息量与上架实物扫描数量匹配逻辑（按 料号+订单号+批次 汇总卡控） quinn -- 取消执行
            //DataTable ldt_sj = LicPdaService.App.GetCreateDataTable();
            //try
            //{
            //    #region 按料号+订单号+批次 汇总
            //    for (int i = 0; i < ldt.Rows.Count; i++)
            //    {

            //        string[] ls;
            //        ls = ldt.Rows[i]["master_label"].ToString().Split('|');
            //        if (ls.Length == 5 || ls.Length == 4)
            //        {
            //            DataRow[] ldrs = null;
            //            string lswhere = "asn='" + App.sasn + "' and sku='" + ls[0].ToString() + "' and lottable02='" + ls[1].ToString() + "' and lottable01='" + ls[2].ToString() + "' ";
            //            ldrs = ldt_sj.Select(lswhere);

            //            if (ldrs.Length == 0)
            //            {
            //                DataRow ldr = ldt_sj.NewRow();
            //                ldr["asn"] = App.sasn;
            //                ldr["sku"] = ls[0].ToString();
            //                ldr["lottable02"] = ls[1].ToString();
            //                ldr["lottable01"] = ls[2].ToString();
            //                if (ls.Length == 5)
            //                {
            //                    ldr["shipkey"] = ls[4].ToString();
            //                }
            //                else
            //                {
            //                    ldr["shipkey"] = ldt_asn.Rows[0]["shipkey"].ToString();
            //                }
            //                ldr["qty"] = Convert.ToSingle(ls[3].ToString());
            //                ldr["master_label"] = ldt.Rows[i]["master_label"].ToString();
            //                ldr["userid"] = App.suserid;
            //                ldr["whseid"] = App.swhseid;
            //                ldt_sj.Rows.Add(ldr);
            //            }
            //            else
            //            {
            //                ldrs[0]["qty"] = Convert.ToSingle(ldrs[0]["qty"].ToString()) + Convert.ToSingle(ls[3].ToString());
            //            }
            //        }
            //    }
            //    #endregion

            //    #region 按料号+订单号+批次 汇总扫描上架数量卡控(新逻辑)
            //    for (int i = 0; i < ldt_sj.Rows.Count; i++)
            //    {
            //        asn = ldt_sj.Rows[i]["asn"].ToString();
            //        sku = ldt_sj.Rows[i]["sku"].ToString(); //料号
            //        lottable01= ldt_sj.Rows[i]["lottable01"].ToString(); //批次
            //        lottable02 = ldt_sj.Rows[i]["lottable02"].ToString(); //订单
            //        qty = Int32.Parse(ldt_sj.Rows[i]["qty"].ToString()); //本次上架数量


            //        strSql = "SELECT line_no,sku,lottable01,udf1,SUM(ISNULL(pre_qty,0.00)) AS qty_yb  FROM [ReceiptDetail]";
            //        strSql += " WHERE   receiptkey='" + asn + "' and whseid='" + App.swhseid + "' and sku='" + sku + "' and lottable01='"+lottable01+"' and udf1='"+lottable02+ "' and pre_qty>0  and (udf4 !='PDA新增预报' or udf4 is null ) GROUP BY line_no,sku,lottable01,udf1";
            //        DataTable ldt_yb = App.GetDataTable(App.DBconWMS, strSql, "ReceiptDetail");

            //        if (ldt_yb.Rows.Count == 1)
            //        {
            //            //预报数量
            //            line_no = ldt_yb.Rows[0]["line_no"].ToString();
            //            qty_yb = Int32.Parse(ldt_yb.Rows[0]["qty_yb"].ToString().Substring(0, ldt_yb.Rows[0]["qty_yb"].ToString().Length - 5));

            //            strSql = "SELECT line_no,sku,SUM(qty) AS qty_sm   FROM RecPackageDetail";
            //            strSql += " WHERE   receiptkey='" + asn + "' and sku='" + sku + "' and line_no='"+line_no+ "' and qty>0  GROUP BY sku,line_no";
            //            DataTable ldt_sm = App.GetDataTable(App.DBconWMS, strSql, "ReceiptDetail");
            //            if (ldt_sm.Rows.Count > 0)
            //            {
            //                //料号已上架数量
            //                qty_sm = Int32.Parse(ldt_sm.Rows[0]["qty_sm"].ToString().Substring(0, ldt_sm.Rows[0]["qty_sm"].ToString().Length - 5));
            //            }
            //            if (qty + qty_sm > qty_yb)
            //            {
            //                for (int j = 0; j < ldt_dy.Rows.Count; j++)
            //                {
            //                    ldt_dy.Rows[j]["msg"] = "验收&上架 失败！！！ 原因：料号:" + sku + " 订单："+lottable02+" 批次："+lottable01+" 的预报维护数量" + qty_yb.ToString() + "小于 累计上架扫描数量总和:" + (qty + qty_sm).ToString() + " - 提示来自 上架(分配包装)数量卡控";
            //                }
            //                return GetPrint_Return(ldt_dy);
            //            }
            //        }
            //        else
            //        {
            //            if(ldt_yb.Rows.Count > 1)
            //            {
            //                for (int j = 0; j < ldt_dy.Rows.Count; j++)
            //                {
            //                    ldt_dy.Rows[j]["msg"] = "验收&上架 失败！！！ 原因：料号：" + sku + " 订单："+lottable02+" 批次："+lottable01+" 的预报信息维护了多条，无法进行PDA验收&上架，无法进行PDA验收&上架 - 提示来自 料号上架卡控";
            //                }
            //                return GetPrint_Return(ldt_dy);
            //            }
            //            else
            //            {
            //                for (int j = 0; j < ldt_dy.Rows.Count; j++)
            //                {
            //                    ldt_dy.Rows[j]["msg"] = "验收&上架 失败！！！ 原因：料号：" + sku + " 订单：" + lottable02 + " 批次：" + lottable01 + " 没有维护预报信息，无法进行PDA验收&上架 - 提示来自 料号上架卡控";
            //                }
            //                return GetPrint_Return(ldt_dy);
            //            }

            //        }
            //    }
            //    #endregion

            //}
            //catch (Exception ex)
            //{
            //    App.Log("上架数量卡控失败：", ex.Message.ToString()+"可能原因：数据格式不正确" + "SQL:"+strSql);
            //    //throw;
            //}
            #endregion

            #region 4.1预报信息量与上架实物扫描数量匹配逻辑（按 料号 汇总卡控） quinn --  执行中 
            DataTable ldt_sj = LicPdaService.App.GetCreateDataTable();
            try
            {
                #region 按料号+订单号+批次 汇总
                for (int i = 0; i < ldt.Rows.Count; i++)
                {

                    string[] ls;
                    ls = ldt.Rows[i]["master_label"].ToString().Split('|');
                    if (ls.Length == 5 || ls.Length == 4)
                    {
                        DataRow[] ldrs = null;
                        string lswhere = "asn='" + App.sasn + "' and sku='" + ls[0].ToString() + "' and lottable02='" + ls[1].ToString() + "' and lottable01='" + ls[2].ToString() + "' ";
                        ldrs = ldt_sj.Select(lswhere);

                        if (ldrs.Length == 0)
                        {
                            DataRow ldr = ldt_sj.NewRow();
                            ldr["asn"] = App.sasn;
                            ldr["sku"] = ls[0].ToString();
                            ldr["lottable02"] = ls[1].ToString();
                            ldr["lottable01"] = ls[2].ToString();
                            if (ls.Length == 5)
                            {
                                ldr["shipkey"] = ls[4].ToString();
                            }
                            else
                            {
                                ldr["shipkey"] = ldt_asn.Rows[0]["shipkey"].ToString();
                            }
                            ldr["qty"] = Convert.ToSingle(ls[3].ToString());
                            ldr["master_label"] = ldt.Rows[i]["master_label"].ToString();
                            ldr["userid"] = App.suserid;
                            ldr["whseid"] = App.swhseid;
                            ldt_sj.Rows.Add(ldr);
                        }
                        else
                        {
                            ldrs[0]["qty"] = Convert.ToSingle(ldrs[0]["qty"].ToString()) + Convert.ToSingle(ls[3].ToString());
                        }
                    }
                }
                #endregion

                #region 料号数量 汇总
                DataTable ldt_sk = LicPdaService.App.GetCreateDataTable();
                for (int i = 0; i < ldt_sj.Rows.Count; i++)
                {
                    string[] ls;
                    ls = ldt_sj.Rows[i]["master_label"].ToString().Split('|');
                    DataRow[] ldrs = null;
                    string lswhere = "asn='" + App.sasn + "' and sku='" + ls[0].ToString() + "'";
                    ldrs = ldt_sk.Select(lswhere);
                    if (ldrs.Length == 0)
                    {
                        DataRow ldr = ldt_sk.NewRow();
                        ldr["asn"] = App.sasn;
                        ldr["sku"] = ls[0].ToString();
                        ldr["qty"] = Convert.ToSingle(ldt_sj.Rows[i]["qty"].ToString());
                        ldr["userid"] = App.suserid;
                        ldr["whseid"] = App.swhseid;
                        ldt_sk.Rows.Add(ldr);
                    }
                    else
                    {
                        ldrs[0]["qty"] = Convert.ToSingle(ldrs[0]["qty"].ToString()) + Convert.ToSingle(ldt_sj.Rows[i]["qty"].ToString());
                    }

                }
                #endregion

                #region 按料号 汇总扫描上架数量卡控(新逻辑)
                for (int i = 0; i < ldt_sk.Rows.Count; i++)
                {
                    asn = ldt_sk.Rows[i]["asn"].ToString();
                    sku = ldt_sk.Rows[i]["sku"].ToString(); //料号
                    qty = double.Parse(ldt_sk.Rows[i]["qty"].ToString()); //本次上架数量
                    qty_sm = 0;
                    qty_yb = 0;


                    strSql = "SELECT line_no,sku,SUM(ISNULL(pre_qty,0.00)) AS qty_yb  FROM [ReceiptDetail]";
                    strSql += " WHERE   receiptkey='" + asn + "' and whseid='" + App.swhseid + "' and sku='" + sku + "' and pre_qty>0  GROUP BY line_no,sku";
                    DataTable ldt_yb = App.GetDataTable(App.DBconWMS, strSql, "ReceiptDetail");

                    if (ldt_yb.Rows.Count == 1)
                    {
                        //预报数量
                        line_no = ldt_yb.Rows[0]["line_no"].ToString();
                        qty_yb = double.Parse(ldt_yb.Rows[0]["qty_yb"].ToString());

                        strSql = "SELECT line_no,sku,SUM(qty) AS qty_sm   FROM RecPackageDetail";
                        strSql += " WHERE   receiptkey='" + asn + "' and sku='" + sku + "' and line_no='" + line_no + "' and qty>0  GROUP BY sku,line_no";
                        DataTable ldt_sm = App.GetDataTable(App.DBconWMS, strSql, "ReceiptDetail");
                        if (ldt_sm.Rows.Count > 0)
                        {
                            //料号已上架数量
                            qty_sm = double.Parse(ldt_sm.Rows[0]["qty_sm"].ToString());
                        }
                        if (qty + qty_sm > qty_yb)
                        {
                            for (int j = 0; j < ldt_dy.Rows.Count; j++)
                            {
                                ldt_dy.Rows[j]["msg"] = "验收&上架 失败！！！ 原因：料号:" + sku + " 的预报维护数量" + qty_yb.ToString() + "小于 累计上架扫描数量总和:" + (qty + qty_sm).ToString() + " - 提示来自 上架(分配包装)数量卡控";
                            }
                            return GetPrint_Return(ldt_dy);
                        }
                    }
                    else
                    {
                        if (ldt_yb.Rows.Count > 1)
                        {
                            for (int j = 0; j < ldt_dy.Rows.Count; j++)
                            {
                                ldt_dy.Rows[j]["msg"] = "验收&上架 失败！！！ 原因：料号：" + sku + " 的预报信息维护了多条，无法进行PDA验收&上架，无法进行PDA验收&上架 - 提示来自 料号上架卡控";
                            }
                            return GetPrint_Return(ldt_dy);
                        }
                        else
                        {
                            for (int j = 0; j < ldt_dy.Rows.Count; j++)
                            {
                                ldt_dy.Rows[j]["msg"] = "验收&上架 失败！！！ 原因：料号：" + sku + " 没有维护预报信息，无法进行PDA验收&上架 - 提示来自 料号上架卡控";
                            }
                            return GetPrint_Return(ldt_dy);
                        }

                    }
                }
                #endregion

            }
            catch (Exception ex)
            {
                App.Log("上架数量卡控失败：", ex.Message.ToString() + "可能原因：数据格式不正确" + "SQL:" + strSql);
                //throw;
            }
            #endregion

            #region 4.2 库位合法性卡控
            for (int k = 0; k < ldt_dy.Rows.Count; k++)
            {
                strSql = "SELECT loc FROM Location where loc='" + ldt_dy.Rows[k]["storage"].ToString() + "' AND whseid='" + App.swhseid + "'";
                DataTable dtlocnew = App.GetDataTable(App.DBconWMS, strSql, "LOC");
                if (dtlocnew.Rows.Count <= 0)
                {
                    ldt_dy.Rows[k]["msg"] = "扫描的库位" + ldt_dy.Rows[k]["storage"].ToString() + "不合法。可能原因：1、库位无效 2、扫描库位不隶属登录账号所在的厂别" + App.swhseid;
                    return GetPrint_Return(ldt_dy);
                }
            }
            #endregion

            #region 获取行号
            int lnline_no = ldt_asn_detail.Rows.Count; //old 保留
            strSql = "SELECT MAX(line_no) AS line_no FROM [ReceiptDetail] WHERE   receiptkey='" + App.sasn + "' and whseid='" + App.swhseid + "'";
            DataTable dtln = App.GetDataTable(App.DBconWMS, strSql, "ReceiptDetail");
            if (dtln.Rows.Count > 0)
            {
                lnline_no = Int32.Parse(dtln.Rows[0]["line_no"].ToString()); //new 更新
            }
            #endregion
             
            try
            {
                #region 按Asn+料号+订单+批次 汇总数量
                DataRow[] ldrs;
                DataTable ldt_ys = LicPdaService.App.GetCreateDataTable();
                for (int i = 0; i < ldt_dy.Rows.Count; i++)
                {
                    ldrs = ldt_ys.Select("sku='" + ldt_dy.Rows[i]["sku"].ToString() + "' and lottable01='" + ldt_dy.Rows[i]["lottable01"].ToString() + "' and lottable02='" + ldt_dy.Rows[i]["lottable02"].ToString() + "'");
                    if (ldrs.Length == 0)
                    {
                        DataRow ldr = ldt_ys.NewRow();
                        ldr["asn"] = App.sasn;
                        ldr["sku"] = ldt_dy.Rows[i]["sku"].ToString();
                        ldr["lottable02"] = ldt_dy.Rows[i]["lottable02"].ToString();
                        ldr["lottable01"] = ldt_dy.Rows[i]["lottable01"].ToString();
                        ldr["qty"] = Convert.ToSingle(ldt_dy.Rows[i]["qty"].ToString());
                        ldr["master_label"] = ldt_dy.Rows[i]["master_label"].ToString();
                        ldr["userid"] = App.suserid;
                        ldr["whseid"] = App.swhseid;
                        ldt_ys.Rows.Add(ldr);
                    }
                    else
                    {
                        ldrs[0]["qty"] = Convert.ToSingle(ldrs[0]["qty"].ToString()) + Convert.ToSingle(ldt_dy.Rows[i]["qty"].ToString());

                    }
                }
                ldt_dy = ldt_ys;  //汇总后赋值ldt_dy 作为后续操作基表
                #endregion

                #region 补加预报信息 - 取消执行
                //for (int i = 0; i < ldt_dy.Rows.Count; i++)
                //{

                //    ldrs = ldt_asn_detail.Select("sku='" + ldt_dy.Rows[i]["sku"].ToString() + "' and lottable01='" + ldt_dy.Rows[i]["lottable01"].ToString() + "' and udf1='" + ldt_dy.Rows[i]["lottable02"].ToString() + "'");
                //    ldt_dy.Rows[i]["lpn"] = lspn;

                //    strSql = "SELECT description FROM [Sku] where sku='" + ldt_dy.Rows[i]["sku"].ToString() + "' and whseid='" + App.swhseid + "'";
                //    DataTable ldt_sku = App.GetDataTable(App.DBconWMS, strSql, "ReceiptDetail");
                //    if (ldt_sku.Rows.Count > 0)
                //    {
                //        lsdesc = ldt_sku.Rows[0]["description"].ToString();
                //    }

                //    if (ldrs.Length == 0)
                //    {
                //        lnline_no += 1;
                //        //无Asn+料号+订单+批次 对应的 预报信息时，补加
                //        string lottable03 = App.sasn.ToString() + lnline_no.ToString().PadLeft(5, '0').ToString();
                //        //string date_now = App.ws.ExecScalar(" select   CONVERT(varchar(100), GETDATE(), 120) ", "").ToString();

                //        string lssql = "insert into ReceiptDetail(whseid,receiptkey,line_no,customer_code,sku,skudescr,lottable09,lottable10,uom,status,pre_qty,unrec_qty,goodqty,lottable01,udf1,lottable03,lottable04,lottable05,addwho,adddate,check_qty,declaration_qty,udf4) values(";
                //        lssql += "'" + App.swhseid + "','" + App.sasn + "','" + lnline_no.ToString().PadLeft(5, '0') + "','" + lscustomer_code + "','" + ldt_dy.Rows[i]["sku"].ToString() + "'";
                //        lssql += ",'" + lsdesc + "','" + lslottable09 + "','" + lslottable10 + "','CTN','01'," + ldt_dy.Rows[i]["qty"] + "," + ldt_dy.Rows[i]["qty"] + "," + ldt_dy.Rows[i]["qty"] + ",'" + ldt_dy.Rows[i]["lottable01"].ToString() + "','" + ldt_dy.Rows[i]["lottable02"].ToString() + "','" + lottable03 + "',CONVERT(varchar(100), GETDATE(), 120),CONVERT(varchar(100), GETDATE(), 120)";
                //        lssql += ",'" + App.suserid + "','" + System.DateTime.Now.ToString() + "',0," + ldt_dy.Rows[i]["qty"] + ",'PDA新增预报')";
                //        sqllists.Add(lssql);
                //    }
                //}
                //if (!App.ExecuteSQLScriptLists(sqllists, App.DBconWMS))
                //{
                //    for (int j = 0; j < ldt_dy.Rows.Count; j++)
                //    {
                //        ldt_dy.Rows[j]["msg"] = "上架失败！！！原因：供应商Mastlabel标签信息（判定条件：料号+订单+批次）与预报不符，系统自动添加ASN预报信息失败！";
                //    }
                //    return GetPrint_Return(ldt_dy);
                //}
                #endregion

                #region 预报信息不准确卡死 无需补加预报信息
                for (int i = 0; i < ldt_dy.Rows.Count; i++)
                {
                    
                    ldrs = ldt_asn_detail.Select("sku='" + ldt_dy.Rows[i]["sku"].ToString() + "' ");
                    ldt_dy.Rows[i]["lpn"] = lspn;

                    if (ldrs.Length == 0)
                    {
                        for (int j = 0; j < ldt_dy.Rows.Count; j++)
                        {
                            ldt_dy.Rows[j]["msg"] = "上架&分配包装失败！！！ 原因：扫描明细中存在Mast_Labeld对应料号没有预报信息。错误的料号："+ ldt_dy.Rows[i]["sku"].ToString();
                        }
                        return GetPrint_Return(ldt_dy);
                    }
                }
                #endregion

                sqllists.Clear();
                ldt_dy = App.GetNewDataTabel(ldt);  //包装信息写入不可汇总，故需要为ldt_dy重新赋值
                for (int i = 0; i < ldt_dy.Rows.Count; i++)
                {
                    #region 参数源
                    strSql = "select * from  ReceiptDetail where (status in ('01','02','03','04','05') OR status IS NULL) and  whseid='" + App.swhseid + "' and receiptkey='" + App.sasn + "'";
                    ldt_asn_detail = App.GetDataTable(App.DBconWMS, strSql, "Sku");
                    ldrs = ldt_asn_detail.Select("sku='" + ldt_dy.Rows[i]["sku"].ToString() + "'");
                    //and lottable01 = '" + ldt_dy.Rows[i]["lottable01"].ToString() + "' and udf1 = '" + ldt_dy.Rows[i]["lottable02"].ToString() + "'
                    ldt_dy.Rows[i]["lpn"] = lspn;
                    if (ldrs.Length == 0)
                    {
                        for (int j = 0; j < ldt_dy.Rows.Count; j++)
                        {
                            ldt_dy.Rows[j]["msg"] = "ASN预报中未发现料。可能原因：1、ASN已全部上架完成，不可重复上架   2、ASN中没有料号"+ ldt_dy.Rows[i]["sku"].ToString() + "的待上架预报信息！";
                        }
                        return GetPrint_Return(ldt_dy);
                    }
                    else
                    {
                        qty = double.Parse((ldt_dy.Rows[i]["qty"].ToString())) ;
                        lsline_no = ldrs[0]["line_no"].ToString();
                        lsdesc = ldrs[0]["skudescr"].ToString();
                        lsunit = ldrs[0]["uom"].ToString();
                        //lsinvoice_no = ldrs[0]["invoice_no"].ToString();
                        lslottable01 = ldrs[0]["lottable01"].ToString();
                        lslottable02 = ldrs[0]["lottable02"].ToString();
                        lslottable03 = ldrs[0]["lottable03"].ToString();
                        lslottable04 = ldrs[0]["lottable04"].ToString();
                        lslottable05 = ldrs[0]["lottable05"].ToString();
                        lslottable06 = ldrs[0]["lottable06"].ToString();
                        lslottable07 = ldrs[0]["lottable07"].ToString();
                        lslottable08 = ldrs[0]["lottable08"].ToString();
                        lslottable09 = ldrs[0]["lottable09"].ToString();   //包装键
                        lslottable10 = ldrs[0]["lottable10"].ToString();   //供应商代码

                        //新增计算 单价/单重/总价 并写入包装明细表
                        try
                        {
                            unit_weight = double.Parse(ldrs[0]["net_weight"].ToString()) / double.Parse(ldrs[0]["pre_qty"].ToString()); //单重

                            net_weight = unit_weight*qty; //净重
                            unit_price = double.Parse(ldrs[0]["unit_price"].ToString()); //单价
                            total_price = double.Parse(ldrs[0]["unit_price"].ToString())* qty; //总价
                        }
                        catch { }

                    }
                    
                    string guid = Guid.NewGuid().ToString();

                    //包装ID
                    string packageid = string.Empty;
                    strSql = "DECLARE @key varchar(50) exec dbo.pro_CreatePackageid '" + App.swhseid + "','P','CreatePackageid','一层包装',@key OUTPUT select @key as packageid";
                    DataTable dtPack = App.GetDataTable(App.DBconWMS, strSql, "Pack");
                    if (dtPack.Rows.Count > 0)
                    {
                        packageid = dtPack.Rows[0]["packageid"].ToString();
                    }

                    //箱号为包装前缀+流水号，如前缀为空则为发票号+流水号，取第一行的发票号
                    string ctn_no = "";
                    strSql = " DECLARE @key varchar(50) exec dbo.pro_GetCtn_no 'wh1','NO','CREATECTNNO','" + App.sasn + "',@key OUTPUT select @key as ctn_no";
                    DataTable dtCtn = App.GetDataTable(App.DBconWMS, strSql, "Pack");
                    if (dtCtn.Rows.Count > 0)
                    {
                        ctn_no = dtCtn.Rows[0]["ctn_no"].ToString();
                    }
                    #endregion

                    #region 数据写入 - 新增 单价/单重/净重/总价 写入
                    strSql = "insert into RecPackage(guid,whseid,receiptkey,p_level,packageid,ctn_no,uom,volume,net_weight";
                    strSql += ",gross_weight,length,width,height,remark,addwho,adddate,editwho,editdate,recstatus,loc,lottable01,udf1) values ";
                    strSql += "('" + guid + "','" + App.swhseid + "','" + App.sasn + "','1','" + packageid + "','" + ctn_no + "'";
                    strSql += ",'CTN',0,"+net_weight+",0,0,0,0,''";
                    strSql += ",'" + App.suserid + "','" + DateTime.Now + "','" + App.suserid + "','" + DateTime.Now + "','','" + storage + "','" + ldt_dy.Rows[i]["lottable01"].ToString() + "','" + ldt_dy.Rows[i]["lottable02"].ToString() + "')";
                    sqllists.Add(strSql);

                    strSql = "insert into RecPackageDetail(guid,m_guid,receiptkey,line_no,sku,skudescr,qty,uom,addwho,adddate,editwho,editdate,declaration_qty,gross_weight,unit_price,total_price,unit_weight,net_weight,lottable01,udf1) values ";
                    strSql += "(newid(),'" + guid + "','" + App.sasn + "','" + lsline_no + "','" + ldt_dy.Rows[i]["sku"].ToString() + "','" + lsdesc + "'," + qty + ",'CTN','" + App.suserid + "','" + DateTime.Now + "'";
                    strSql += ",'" + App.suserid + "','" + DateTime.Now + "'," + qty + ",0," + unit_price + "," + total_price + "," + unit_weight + "," + net_weight + ",'" + ldt_dy.Rows[i]["lottable01"].ToString() + "','" + ldt_dy.Rows[i]["lottable02"].ToString() + "')";
                    sqllists.Add(strSql);
                    #endregion
                }

                if (!App.ExecuteSQLScriptLists(sqllists, App.DBconWMS))
                {
                    #region
                    App.Log("SQL语句执行失败：", strSql.ToString());
                    for (int j = 0; j < ldt_dy.Rows.Count; j++)
                    {
                        ldt_dy.Rows[j]["msg"] = "上架失败！！！原因：SQL语句执行失败！！！";
                    }
                    return GetPrint_Return(ldt_dy);
                    #endregion
                }

            }
            catch (Exception ex)
            {
                #region
                App.Log("Print1", ex.Message.ToString());
                for (int j = 0; j < ldt_dy.Rows.Count; j++)
                {
                    ldt_dy.Rows[j]["msg"] = ex.ToString();
                }
                return GetPrint_Return(ldt_dy);
                //throw;
                #endregion
            }

            return GetPrint_Return(ldt_dy);
        }

        //条印--返回值
        private static string GetPrint_Return(DataTable ldt)
        {
            #region 排除重复项
            for (int i = ldt.Rows.Count - 2; i > 0; i--)
            {
                DataRow[] ldrs = ldt.Select("sku='" + ldt.Rows[i]["sku"].ToString() + "'");
                if (ldrs.Length > 1)
                {
                    ldt.Rows.RemoveAt(i);
                }
            }
            ldt.AcceptChanges();
            XmlDocument doc = new XmlDocument();//返回报文
            XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
            doc.AppendChild(dec);
            XmlElement rootRet = doc.CreateElement("NewDataSet");
            #endregion

            #region 构建报文
            for (int i = 0; i < ldt.Rows.Count; i++)
            {
                XmlNode printNodes = doc.CreateElement("print_return");//返回用户节点
                XmlElement lxeuserid = doc.CreateElement("userid");
                XmlElement lxeasn = doc.CreateElement("asn");
                XmlElement lxewhseid = doc.CreateElement("whseid");
                XmlElement lxemaster_label = doc.CreateElement("master_label");
                XmlElement lxestorage = doc.CreateElement("storage");
                XmlElement lxereturn_status = doc.CreateElement("return_status");
                XmlElement lxesku = doc.CreateElement("sku");
                XmlElement lxebatch = doc.CreateElement("batch");
                XmlElement lxelpn = doc.CreateElement("lpn");
                XmlElement lxeqty = doc.CreateElement("qty");
                XmlElement lxepage = doc.CreateElement("page");

                lxeuserid.InnerText = ldt.Rows[i]["userid"].ToString();
                lxeasn.InnerText = ldt.Rows[i]["asn"].ToString();
                lxewhseid.InnerText = ldt.Rows[i]["whseid"].ToString();
                lxemaster_label.InnerText = ldt.Rows[i]["master_label"].ToString();
                lxestorage.InnerText = ldt.Rows[i]["storage"].ToString();
                lxereturn_status.InnerText = ldt.Rows[i]["msg"].ToString();
                lxesku.InnerText = ldt.Rows[i]["sku"].ToString();
                lxeqty.InnerText = ldt.Rows[i]["qty"].ToString();
                lxebatch.InnerText = ldt.Rows[i]["lottable01"].ToString();
                lxelpn.InnerText = ldt.Rows[i]["lpn"].ToString();
                lxepage.InnerText = Math.Ceiling(Convert.ToDecimal(i + 1) / 1).ToString();

                printNodes.AppendChild(lxeuserid);
                printNodes.AppendChild(lxeasn);
                printNodes.AppendChild(lxewhseid);
                printNodes.AppendChild(lxemaster_label);
                printNodes.AppendChild(lxereturn_status);
                printNodes.AppendChild(lxestorage);
                printNodes.AppendChild(lxesku);
                printNodes.AppendChild(lxeqty);
                printNodes.AppendChild(lxebatch);
                printNodes.AppendChild(lxelpn);
                printNodes.AppendChild(lxepage);
                rootRet.AppendChild(printNodes);
            }
            #endregion

            doc.AppendChild(rootRet);
            return doc.InnerXml;
        }
    }
}
